﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore.Internal;

namespace net.xBei.Models.FilterCondition {
    public class ConditionItem {
        /// <summary>
        /// 条件
        /// </summary>
        public List<ConditionItem> Conditions { get; set; } = default!;
        /// <summary>
        /// 字段名
        /// </summary>
        public string Field { get; set; } = default!;
        /// <summary>
        /// 
        /// </summary>
        public ConditionRange Range { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 操作
        /// </summary>
        public string Operator { get; set; } = default!;
        /// <summary>
        /// 结果（条件的计算结果）
        /// </summary>
        public dynamic Result { get; set; }
        /// <summary>
        /// 可用的操作列表
        /// </summary>
        public static Dictionary<string, string> OperatorList = new() {
            {"=", "等于"},
            {"!=", "不等于"},
            {">", "大于"},
            {"<", "小于"},
            {">=", "不小于"},
            {"<=", "不大于"},
            {"()", "包含"},
            {"!()", "不包含"},
            {"{}", "属于"},
            {"!{}", "不属于"},
            {"1", "真"},
            {"0", "假"},
            {"any", "之一"},
            {"all", "全"}
        };
        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public bool IsValid(int level = 0) {
            if (level == 0 && (Conditions.Count < 1 || Conditions.All(cc => cc.IsValid(level + 1)) == false)) {
                return false;
            }
            if (level > 0 && OperatorList.ContainsKey(Operator) == false) {
                return false;
            }
            if (level > 0 && string.IsNullOrWhiteSpace(Field)) {
                return false;
            }
            if (Result == null) {
                return false;
            }

            return true;
        }
    }
    /// <summary>
    /// 满足条件
    /// </summary>
    public enum ConditionRange {
        /// <summary>
        /// 任意
        /// </summary>
        Any = 1,
        /// <summary>
        /// 所有
        /// </summary>
        All
    }

}
